/*
 * UartTool.h
 *
 *  Created on: Aug 17, 2025
 *      Author: lydia
 */

#ifndef SRC_UARTTOOL_H_
#define SRC_UARTTOOL_H_

#include "stdio.h"
#include <cstdint>
#include "stm32f1xx_hal.h"

#define RX_BUFFER_SIZE 2
#define RECEIVER_BUFFER_SIZE 2
#define CMD_FLAG 0xff
#define MY_DEVICE_ID 3


typedef struct
{
	uint8_t seq;
	uint8_t device_id;
	uint8_t value[28] ;
} TLV_VAL;

typedef struct
{

  uint8_t cmd;
  //最多30个字节
  uint8_t cmd_len;
  TLV_VAL value;

  // 0, 空闲未使用;
  // 20 接收数据中; 21 接收完成; 22 已经解释执行命令
  // 30 已经发送，未收到回应；31 已经发送，且收到回应
  uint8_t buffer_status;
  //首次发送消息的时间，ms。
  uint32_t send_time_ms;
} TLVRequest ;


class UartTool
{
public:
	UartTool(UART_HandleTypeDef *_p_uart);

	void UART_send_bytes(uint8_t *p, int my_len);
	void UART_send_str(char *p);
	void loop_deal_rcv_buffer();
	void dma_callback();
	virtual void do_command(int buffer_pos, uint8_t cmd_id) = 0   ;


protected:
	uint8_t rx_buffer[RX_BUFFER_SIZE]={0};
	TLVRequest cmd_receive_buffer[RECEIVER_BUFFER_SIZE];
	uint8_t buffer_receive_pos=0;

	// 0, 待触发；1，已经收到起始符；2 已经收到CMD_request; 3已经收到长度；4 接收完整Value
	uint8_t receive_status = 0 ;
	UART_HandleTypeDef *p_uart;




private:


	// 串口接受状态机
	uint8_t uart_receive_status = 0;



	uint8_t rx_byte;

};

#endif /* SRC_UARTTOOL_H_ */
